from django.contrib.auth.models import User
from django.contrib.auth import login

def get_user(request):
    if request.user.is_authenticated():
        user = request.user
    else:
        user = User.objects.get(pk=2) #Anonym
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)

    return user

def is_anonymous(user):
    if user is None:
        return False
    return user.pk == 2 #Anonym

def new_user(request):
    def random_string(size):
        import random
        characterSet = 'abcdefghijklmnopqrstuvwxyz0123456789_'
        return ''.join([random.choice(characterSet) for i in range(size)])

    user = User.objects.create_user(random_string(12), '', random_string(32))
    user.is_active = False
    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)

    return user
